using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MuscleFellow.Data.Interfaces;
using MuscleFellow.Models;

namespace MuscleFellow.Data.Repositories
{
    public class ProductRepository : IProductRepository
    {
        private readonly MuscleFellowDbContext _dbContext;

        public ProductRepository(MuscleFellowDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task<List<Product>> GetAllAsync()
        {
            return await _dbContext.Products.ToListAsync();
        }

        public async Task<Guid> AddAsync(Product product)
        {
            if (null==product)
            {
                return Guid.Empty;
            }
            _dbContext.Products.Add(product);
            await _dbContext.SaveChangesAsync();
            return product.ProductID;
        }

        public async Task DeleteAsync(Guid productID)
        {
            var product = await _dbContext.Products.SingleOrDefaultAsync(p=>p.ProductID==productID);
            if (null!=product)
            {
                _dbContext.Products.Remove(product);
                await _dbContext.SaveChangesAsync();
            }
        }

        public async Task<IEnumerable<Product>> GetProductsAsync(string filter, int pageSize, int pageCount)
        {
            var result =await _dbContext.Products.Where(p=>String.IsNullOrEmpty(filter)||
                                                           p.ProductName.Contains(filter)||
                                                           p.Description.Contains(filter))
                .Select(p=>new {Product=p})
                .Skip(pageSize*pageCount)
                .Take(pageSize)
                .ToListAsync();
            return result.Select(p=>p.Product);
        }

        public async Task<Product> GetAsync(Guid productID)
        {
            return await _dbContext.Products.SingleOrDefaultAsync(p=>p.ProductID==productID);
        }

        public async Task<IEnumerable<Product>> GetPopularProductsAsync(int count)
        {
            var result = await _dbContext.Products
                .Select(p=>new {Product=p})
                .Take(count)
                .ToListAsync();
            return result.Select(p => p.Product);
        }

        public async Task<Guid> UpdateAsync(Product product)
        {
            if (null==product)
            {
                return Guid.Empty;
            }
            _dbContext.Products.Update(product);
            await _dbContext.SaveChangesAsync();
            return product.ProductID;
        }
    }
}